package com.haojiang.mh.remote.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.haojiang.mh.ui.base.BaseActivity
import org.jetbrains.anko.AnkoLogger

/**
 * 所有fragment的基类
 */
abstract class BaseFrag : Fragment(), AnkoLogger {

    protected var rootView: View? = null
    protected lateinit var context: BaseActivity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.context = activity as BaseActivity
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (onLayoutId() == 0) {
            throw RuntimeException("Layout files can not be empty")
        }
        if (rootView == null) {
            rootView = inflater.inflate(onLayoutId(), container, false)
        }
        return rootView
    }

    protected abstract fun onLayoutId(): Int

//    override fun onCreateView(
//        inflater: LayoutInflater,
//        container: ViewGroup?,
//        savedInstanceState: Bundle?
//    ): View? {
//        return initView()
//    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initView()
    }

    open  protected fun initView() {
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initListener()
        initData()
    }

    /**
     * adapter 和 listener
     */
    open protected fun initListener() {
    }

    /**
     * 数据初始化
     */
    open protected fun initData() {
    }

    override fun onDestroy() {
        initDestroy()
        super.onDestroy()
    }

    open protected fun initDestroy() {

    }

    fun showToast(msg: String) {
        context.showToast(msg)
    }
}